package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.common.model.request.LinkErpRequest;
import com.ytjj.qmyx.supplychain.common.model.response.LinkErpResponse;
import com.ytjj.qmyx.supplychain.mapper.BankerMapper;
import com.ytjj.qmyx.supplychain.mapper.LinkErpMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.LinkErp;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  */
@Slf4j
@Service
@RefreshScope
public class LinkErpService {
    @Resource
    private LinkErpMapper linkErpMapper;
    @Resource
    private BankerMapper bankerMapper;

    public LinkErpResponse getByBankId(Integer bankId) {
        LinkErp linkErp = linkErpMapper.getByBankId(bankId);
        LinkErpResponse response = new LinkErpResponse();
        if (linkErp == null) {
            return response;
        }
        BeanUtils.copyProperties(linkErp,response);
        YxBanker yxBanker = bankerMapper.selectByPrimaryKey(bankId);
        response.setErpId(yxBanker.getErpId());
        return response;
    }

    /**
     * 批量获取erp配置
     * @param bankerIds
     * @return
     */
    public List<LinkErpResponse> getByBankIds(List<Integer> bankerIds) {
        if (CollectionUtils.isEmpty(bankerIds)){
            return new ArrayList<>();
        }
        List<LinkErp> linkErps = linkErpMapper.getByBankIds(bankerIds);
        if (CollectionUtils.isEmpty(linkErps)) {
            return new ArrayList<>();
        }
        List<LinkErpResponse> responses = new ArrayList<>();
        linkErps.stream().forEach(linkErp -> {
            LinkErpResponse response = new LinkErpResponse();
            BeanUtils.copyProperties(linkErp,response);
            YxBanker yxBanker = bankerMapper.selectByPrimaryKey(linkErp.getBankerId());
            response.setErpId(yxBanker.getErpId());
            responses.add(response);
        });
        return responses;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(LinkErpRequest request) throws Exception{
        if (request.getId() ==null) {
            LinkErp linkErp = new LinkErp();
            BeanUtils.copyProperties(request, linkErp);
            linkErpMapper.insert(linkErp);
            bankerMapper.updateErpId(linkErp.getBankerId(),request.getErpId());
        } else {
            LinkErp linkErp = new LinkErp();
            BeanUtils.copyProperties(request, linkErp);
            linkErpMapper.updateByPrimaryKey(linkErp);
            bankerMapper.updateErpId(linkErp.getBankerId(),request.getErpId());
        }

    }

    public Integer updateStatus(Integer id, Integer status) {
        LinkErp linkErp = linkErpMapper.selectByPrimaryKey(id);
        if(linkErp != null) {
            linkErp.setStatus(status);
           return linkErpMapper.updateStatus(id,status);
        }
        return 0;
    }
}
